
********************************************************************************
*********************************pooling****************************************
********************************************************************************


use "$off\Pool\pooled.dta", clear

global year		04 05 06 07 08 09 10 11 12 13 14 15 16 17 18

*Household data (H)

	rename year HB010
	rename country HB020 
	rename hhid HB030 

	foreach x in $year {
		di "Year: 20`x'"
		qui merge m:1 HB010 HB020 HB030 using /// 
		"20`x'/H`x'.dta", ///
		keepusing($hvar) update
		drop if _merge==1 & HB010==20`x' 
		drop if _merge==2 & HB010==20`x' 
		drop _merge
	}

	rename HB010 year 
	rename HB020 country 
	rename HB030 hhid 
	

**Personal register (R)

	rename year RB010
	rename country RB020 
	rename hhid RX030 
	rename persid RB030

	foreach x in $year {
		di "Year: 20`x'"
		qui merge m:1 RB010 RB020 RX030 RB030 using /// 
		"20`x'/R`x'.dta", ///
		keepusing($rvar) update
		drop if _merge==1 & RB010==20`x' 
		drop if _merge==2 & RB010==20`x' 
		drop _merge
	}
	
	*Add information on length of residency (RB031)
	
	foreach x in 10 11 12 13 14 15 16 17 18 {
	di "Year: 20`x'"
	qui merge 1:1 RB010 RB020 RX030 RB030 using /// 
	"20`x'\R`x'.dta", ///
	keepusing(RB031*) update
	drop if _merge==1 & RB010==20`x'  
	drop if _merge==2 & RB010==20`x'  
	drop _merge
	}	

	rename RB010 year 
	rename RB020 country 
	rename RB030 persid 
	rename RX030 hhid


*Personal Data (P)

	rename year PB010
	rename country PB020 
	rename hhid PX030 
	rename persid PB030

	foreach x in $year {
		di "Year: 20`x'"
		qui merge m:1 PB010 PB020 PX030 PB030 using /// 
		"20`x'/P`x'.dta", ///
		keepusing($pvar) update
		tab _merge
		drop if _merge==1 & PB010==20`x' 
		drop if _merge==2 & PB010==20`x' 
		drop _merge
	}

	rename PB010 year 
	rename PB020 country 
	rename PB030 persid
	rename PX030 hhid



*Saving

save "pdhr.dta", replace
